
package com.oahc.magicdefender.manager;

import java.util.HashMap;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Array;
import com.oahc.magicdefender.data.EnemyAniData;
import com.oahc.magicdefender.util.EnemyStatus;

public class Enemy extends Actor {

    HashMap<EnemyStatus, Animation> enemyAnimation = new HashMap<EnemyStatus, Animation>();

    private EnemyStatus status = EnemyStatus.STATUS_RUN;

    float stateTime;

    private int blood=100, speed = 1;

    TextureRegion currentFrame;

    public Enemy(Texture monster1Texture, HashMap<String, EnemyAniData> enemyAniDatas) {
        String keyName = "duyanguai_run_huang_000%d.png";
        Array<TextureRegion> runRegionArray = new Array<TextureRegion>();
        for (int i = 1; i <= 8; i++) {
            EnemyAniData monster1AniData = enemyAniDatas.get(String.format(keyName, i));
            TextureRegion monster1Region = new TextureRegion(monster1Texture,
                    monster1AniData.getX(), monster1AniData.getY(), monster1AniData.getWidth(),
                    monster1AniData.getHeight());
            runRegionArray.add(monster1Region);
        }
        Animation animation = new Animation(0.125f, runRegionArray, Animation.LOOP);
        enemyAnimation.put(EnemyStatus.STATUS_RUN, animation);
        
        setX(Gdx.graphics.getWidth());
    }

    @Override
    public void draw(SpriteBatch batch, float parentAlpha) {
        // TODO Auto-generated method stub
        super.draw(batch, parentAlpha);
        stateTime += Gdx.graphics.getDeltaTime();

        currentFrame = enemyAnimation.get(getStatus()).getKeyFrame(stateTime);

        batch.draw(currentFrame, getX(), getY());

    }

    @Override
    public void act(float delta) {
        // TODO Auto-generated method stub
        super.act(delta);

        if (getBlood() <= 0) {
            setStatus(EnemyStatus.STATUS_DEAD);
        }
        if (getX() <= 0) {
            setVisible(false);
        }

        EnemyStatus status = getStatus();
        if (EnemyStatus.STATUS_DEAD.equals(status)) {
            setVisible(false);
        } else if (EnemyStatus.STATUS_RUN.equals(status)) {
            setX(getX() - speed);
        } else if (EnemyStatus.STATUS_ATTACK.equals(status)) {

        }
    }

    public EnemyStatus getStatus() {
        return status;
    }

    public void setStatus(EnemyStatus status) {
        this.status = status;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}
